/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.elf.gwt.common.client.model;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONValue;

/**
 *  
 */
public class ShippingModel extends AbstractModel
{
	//[{"weight":31.25,"num_boxes":2,"module":"usps","tax_rate":6,"handling":0,
	//"rates":[{"service":"EXPRESS","postage":"64.85"},{"service":"PRIORITY","postage":"19.50"},{"service":"PARCEL","postage":"15.99"}]}
	
	protected double weight;
	protected int numBoxes;
	protected String module;
	protected double taxRate;
	protected double handling;
	protected List<ShippingRateModel> rates;
	
	public ShippingModel()
	{
	}

	public ShippingModel(JSONObject jsonObject)
	{
		weight = convertToDouble(jsonObject,"weight");
		numBoxes = convertToInteger(jsonObject,"num_boxes");
		module = convertToString(jsonObject,"module");
		taxRate = convertToDouble(jsonObject, "tax_rate");
		handling = convertToDouble(jsonObject, "handling");

		rates = new ArrayList<ShippingRateModel>();
		buildRates(jsonObject);
	}

	protected void buildRates(JSONObject jsonObject)
	{
		rates.clear();

		try {
			JSONValue rateValue = jsonObject.get("rates");

			if (rateValue != null)
			{
				JSONArray jsonArray;
				if ((jsonArray = rateValue.isArray()) != null)
				{

					int sz = jsonArray.size();		
					for (int i = 0; i < sz; i++)
					{
						JSONValue jsonValue = jsonArray.get(i);
						JSONObject jsonRObject;
						if ((jsonRObject = jsonValue.isObject()) != null)
						{
							ShippingRateModel ratesModel = new ShippingRateModel(jsonRObject);
							rates.add(ratesModel);
						}
					}
				}
			}
		}
		catch (Exception e) {}
	}

	public void update(ShippingModel model)
	{
		weight = model.getWeight();
		numBoxes = model.getNumBoxes();
		module = model.getModule();
		taxRate = model.getTaxRate();
		rates = model.getRates();
	}

	public double getHandling() {
		return handling;
	}

	public String getModule() {
		return module;
	}

	public double getTaxRate() {
		return taxRate;
	}

	public int getNumBoxes() {
		return numBoxes;
	}

	public double getWeight() {
		return weight;
	}

	public List<ShippingRateModel> getRates() {
		return rates;
	}
}
